﻿var refID;

$(function () {
    //初始化列表信息
    init();
    refID = $("#refID").val();
});

//初始化列表信息
function init(queryData) {
    //绑定树
    var treeDatas = [{ id: "1", text: "我负责的项目" }, { id: "2", text: "我参与的项目" }];
    $('#tree').tree({
        animate: true,
        data: treeDatas,
        onClick: function (node) {
            doSearch();
        }
    });

    $('#list').datagrid({
        title: '',
        idField: 'ProjectID',
        fitColumns: 'true',
        autoRowHeight: true,
        nowrap: true,
        striped: true,
        pagination: true,
        singleSelect: true,
        url: 'Project/GetList',
        view: projectView,
        showHeader: false,
        columns: [[
			//{ field: 'ck', checkbox: true },
			{ field: 'ProjectID', title: 'ProjectID', hidden: true },
			{ field: 'ProjectName', title: '项目名称', width: 180 },
			//{ field: 'ProjectCode', title: '项目编号', width: 180 },
			//{ field: 'StartTime', title: '开始时间', width: 180 },
			{
			    field: 'FinishTime', title: '完成时间', width: 180,
			    formatter: function (value, row, index) {
			        return formatDate(value, "yyyy-MM-dd");
			    }
			},
			//{ field: 'ProjectLevel', title: '项目级别', width: 180, hidden: true },
			{ field: 'FinishPercent', title: '完成度(%)', width: 120 },
			{ field: 'ManageUserIDName', title: '负责人', width: 180 },
			//{ field: 'JoinUserIDs', title: '参与人(多）', width: 180 },

        ]],

        onClickRow: function (rowIndex, rowData) {
            if (rowData) {
                //绑定项目详情信息
                hidProjectID = rowData["ProjectID"];
                $('#hidProjectID').val(rowData["ProjectID"]);
                $('#name').html(rowData["ProjectName"]);
                $('#level').html(rowData["ProjectLevelName"]);
                $('#percent').html(rowData["FinishPercent"]);
                $('#finishTim').html(formatDate(rowData["FinishTime"], "yyyy-MM-dd"));
                $('#managerUser').html(rowData["ManageUserIDName"]);
                $('#joinUser').html(rowData["JoinUserIDsName"]);
                $('#desc').html(rowData["Description"]);
                //初始化项目交流信息
                initChat();
            }
        }
    });
}

//弹出增加窗口
function add() {
    $('#fm').form('clear');
    $('#dlg').dialog('open').dialog('setTitle', '增加');
    $('#refID').val(refID);//把controller里传来的refID赋值到表单隐藏控件中，使提交表单时能带到controller
    $('#frmUploadFile').attr('src', 'Attachment/Index?refID=' + refID);
    url = '/Project/Create';
}

//弹出修改窗口
function edit() {
    var row = $('#list').datagrid('getSelected');
    if (row) {
        row["StartTime"] = formatDate(row["StartTime"], "yyyy-MM-dd");
        row["FinishTime"] = formatDate(row["FinishTime"], "yyyy-MM-dd");
        $('#fm').form('load', row);
        $('#dlg').dialog('open').dialog('setTitle', '修改');
        $('#frmUploadFile').attr('src', 'Attachment/Index?refID=' + row.ProjectID);
        url = '/Project/Edit';
    }
}

//保存数据
function save() {
    $('#fm').form('submit', {
        url: url,
        onSubmit: function () {
            keditor.sync();// 编辑器值回写，这里的keditor 要和 div的  data-options 里建立的一样；
            return $(this).form('validate');
        },
        success: function (result) {
            if (result == 'OK') {
                $('#dlg').dialog('close');      // close the dialog  
                $('#list').datagrid('reload');    // reload the user data  
            } else {
                $.messager.show({
                    title: 'Error',
                    msg: result
                });
            }
        }
    });
}

//删除
function del() {
    var rows = $('#list').datagrid('getSelected');
    if (rows) {
        $.messager.confirm(delDlgTitle, delDlgMsg, function (r) {
            if (r) {
                $.post('/Project/Delete', { id: rows.ProjectID },
                    function (result) {
                        if (result == 'OK') {
                            $("#list").datagrid('clearSelections');
                            $('#list').datagrid('reload');    // reload the user data  
                        } else {
                            $.messager.show({   // show error message  
                                title: 'Error',
                                msg: result
                            });
                        }
                    });
            }
        });
    }
}

//搜索
function doSearch() {
    //alert($("#QStatus").find("option:selected").text());
    var node = $('#tree').tree('getSelected');
    var treeTypeID = "";
    if (node)
        treeTypeID = node.id;
    $('#list').datagrid('load', {
        treeType: treeTypeID,
        projectName: $('#ProjectName').val(),
    });
}

var hidProjectID = $('#hidProjectID').val();
function addChat() {
    $('#dlgChat').dialog('open').dialog('setTitle', '增加');
    hidProjectID = $('#hidProjectID').val();
    $('#fmChat').form('clear');
    url = '/ProjectChat/Create';
}

////保存数据
//function save() {
//    $('#fmChat').form('submit', {
//        url: '/ProjectChat/Create',
//        onSubmit: function () {
//            keditorChat.sync();// 编辑器值回写，这里的keditor 要和 div的  data-options 里建立的一样；
//            return $(this).form('validate');
//        },
//        success: function (result) {
//            if (result == 'OK') {
//                $('#dlg').dialog('close');      // close the dialog  
//                $('#list').datagrid('reload');    // reload the user data  
//            } else {
//                $.messager.show({
//                    title: 'Error',
//                    msg: result
//                });
//            }
//        }
//    });
//}

//保存数据
function saveChat() {

    keditorChat.sync();// 编辑器值回写，这里的keditor 要和 div的  data-options 里建立的一样；
    //验证表单是否通过
    var validate = $('#fmChat').form('validate');
    if (validate == false) {
        return false;
    }

    //构造传递的参数
    var postData = {
        ProjectID: hidProjectID,
        ChatTitle: $('#ChatTitle').val(),
        ChatContent: $('#ChatContent').val()
    }

    //异步传递数据到前台进行添加
    $.post("/ProjectChat/Create", postData, function (data) {
        if (data == "OK") {
            //$.messager.alert("友情提示", "添加成功");
            //关闭层，刷新表单
            $("#fmChat").dialog('close');
            $('#listChat').datagrid('reload');
        }
        else {
            $.messager.show({
                title: 'Error',
                msg: data
            });
        }
    });
}

//初始化项目交流
function initChat() {
    $('#listChat').datagrid({
        title: '',
        idField: 'ProjectChatID',
        fitColumns: 'true',
        autoRowHeight: true,
        nowrap: true,
        striped: true,
        pagination: true,
        singleSelect: true,
        view: projectChatView,
        showHeader: false,
        url: 'ProjectChat/GetList?ProjectID=' + hidProjectID,
        columns: [[
			{ field: 'ProjectChatID', title: 'ProjectChatID', hidden: true },
			{ field: 'ProjectID', title: 'ProjectID', width: 180, hidden: false },
			{ field: 'ChatTitle', title: '标题', width: 50 },
			{ field: 'ChatContent', title: '内容', width: 50 }
			//{ field: 'ParentChatID', title: '上级ID(回复)', width: 180},
			//{ field: 'CreateBy', title: '创建人', width: 180},
			//{ field: 'CreateTime', title: '创建时间', width: 180},
			//{ field: 'UpdateBy', title: '修改人', width: 180},
			//{ field: 'UpdateTime', title: '修改时间', width: 180},

        ]],
    });
}

//project显示列表视图
var projectView = $.extend({}, $.fn.datagrid.defaults.view, {
    renderRow: function (target, fields, frozen, rowIndex, rowData) {
        var cc = [];
        cc.push('<td colspan=' + fields.length + ' style="padding:10px 5px;border:0;width:780px;border-bottom:5px normal">');
        if (!frozen) {
            cc.push('<div class="titleTips">');
            cc.push('<p><span class="c-label">项目名称:</span> <span style="padding-left:5px">' + rowData["ProjectName"] + '</span> <span style="float:right;font-weight:bold">[' + rowData["FinishPercent"] + '%]</span></p>');
            cc.push('<p><span class="c-label" style="float:left">负责人:</span><span style="padding-left:5px">' + rowData["ManageUserIDName"] + '</span><span class="c-label" style="float:right">完成时间:<span style="padding-left:5px;">' + formatDate(rowData["FinishTime"], "yyyy-MM-dd") + '</span></span></p>');
            cc.push('</div>');
            cc.push('<hr style="border:1px dashed black; height:1px">');
        }
        cc.push('</td>');
        return cc.join('');
    }
});

//projectChat显示列表视图
var projectChatView = $.extend({}, $.fn.datagrid.defaults.view, {
    renderRow: function (target, fields, frozen, rowIndex, rowData) {
        var cc = [];
        cc.push('<td colspan=' + fields.length + ' style="padding:10px 5px;border:0;width:780px;border-bottom:5px normal">');
        if (!frozen) {
            cc.push('<div class="titleTips">');
            cc.push('<p><span class="c-label" style="font-weight:bold">[' + rowData["CreateByName"] + ']</span> <span style="padding-left:5px">' + formatDate(rowData["CreateTime"], "yyyy-MM-dd HH:mm") + '</span></p>');
            cc.push('<p><span class="c-label" style="float:left">' + rowData["ChatContent"] + '</span></p>');
            cc.push('</div>');
            cc.push('</br><hr style="border:1px dashed black; height:1px">');
        }
        cc.push('</td>');
        return cc.join('');
    }
});


